package com.ifast.hnzk.controller;


import java.util.Arrays;
import java.util.Date;
import java.util.List;

import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.ifast.common.utils.HttpContextUtils;
import com.ifast.common.utils.IPUtils;
import com.ifast.common.utils.JSONUtils;
import com.ifast.hnzk.domain.ExampleDO;
import com.ifast.hnzk.service.ExampleService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.ifast.common.annotation.Log;
import com.ifast.common.base.BaseController;
import com.ifast.common.utils.Result;

/**
 * 
 * <pre>
 * 
 * </pre>
 * <small> 2019-06-15 12:01:27 | wch</small>
 */
@Controller
@RequestMapping("/hnzk/example")
public class ExampleController extends BaseController {
	@Autowired
	private ExampleService exampleService;
	
	@GetMapping()
	@RequiresPermissions("hnzk:example:example")
	String Example(){
	    return "hnzk/example/example";
	}
	
	@ResponseBody
	@GetMapping("/list")
	@RequiresPermissions("hnzk:example:example")
	public Result<Page<ExampleDO>> list(ExampleDO exampleDTO){
        Wrapper<ExampleDO> wrapper = new EntityWrapper<ExampleDO>().orderBy("id", false);
        Page<ExampleDO> page = exampleService.selectPage(getPage(ExampleDO.class), wrapper);
        return Result.ok(page);
	}
	
	@GetMapping("/add")
	@RequiresPermissions("hnzk:example:add")
	String add(){
		System.out.println("去到新增页面");
	    return "hnzk/example/add";
	}

	@GetMapping("/edit/{id}")
	@RequiresPermissions("hnzk:example:edit")
	String edit(@PathVariable("id") Long id,Model model){
		ExampleDO example = exampleService.selectById(id);
		model.addAttribute("example", example);
	    return "hnzk/example/edit";
	}
	
	@Log("添加")
	@ResponseBody
	@PostMapping("/save")
	@RequiresPermissions("hnzk:example:add")
	public Result<String> save( ExampleDO example){
		example.setCreatetime(new Date());
		exampleService.insert(example);
        return Result.ok();
	}
	
	@Log("修改")
	@ResponseBody
	@RequestMapping("/update")
	@RequiresPermissions("hnzk:example:edit")
	public Result<String>  update( ExampleDO example){
		boolean update = exampleService.updateById(example);
		return update ? Result.ok() : Result.fail();
	}
	
	@Log("删除")
	@PostMapping( "/remove")
	@ResponseBody
	@RequiresPermissions("hnzk:example:remove")
	public Result<String>  remove( Long id){
		exampleService.deleteById(id);
        return Result.ok();
	}
	
	@Log("批量删除")
	@PostMapping( "/batchRemove")
	@ResponseBody
	@RequiresPermissions("hnzk:example:batchRemove")
	public Result<String>  remove(@RequestParam("ids[]") Long[] ids){
		exampleService.deleteBatchIds(Arrays.asList(ids));
		return Result.ok();
	}

	@Log("获取所以列表")
	@RequestMapping("getAllList")
	@ResponseBody
	public Result<List<ExampleDO>> getAllList(){
		List<ExampleDO> list = exampleService.listType();
		System.out.println(JSONUtils.beanToJson(list));
		System.out.println(IPUtils.getIpAddr(HttpContextUtils.getHttpServletRequest()));

		return Result.ok(list);
	}
	
}
